/*
 *  ItemList.cpp
 *  SquirrelAdven
 *
 *  Created by James Weaver on 3/15/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "ItemList.h"
#include "fstream"
#include <string.h>


ItemList::ItemList(string filename) {
	this->filename = filename;
	parseData();
}

ItemList::~ItemList() {
}

void ItemList::parseData() {
	ifstream i_file;
	i_file.open(filename.c_str());
	
	string str;
	char *token;
	while (getline(i_file, str)) {
		// Item
		if (str.find(":Item") != -1) {
			// DEBUG
			std::cout << "find Item data\n";
			// Read each Item data block
			Items * item;
			
			while (getline(i_file, str) && str.find(":") == -1){
				if (str[str.length()-1] == '\r') // there is a \r in my line!
					str.erase(str.length()-1); // throw it away
				// BEGIN
				if (str.find("begin") != -1) {
					item = new Items;
					continue;
				}
				// ID
				if (str.find("ID") != -1) {
					int begin_index = str.find("ID")+strlen("ID");
					str = str.substr(begin_index, str.length()-begin_index);
					// convert to int
					item->setItemID(atoi(str.c_str()));
					continue;
				}
				
				// TITLE
				if (str.find("Title") != -1) {
					int begin_index = str.find("Title")+strlen("Title")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					item->setItemTitle(str);
					continue;
				}
				
				
				// Type Number
				if (str.find("Type") != -1) {
					int begin_index = str.find("Type")+strlen("Type")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					item->setItemType(atoi(str.c_str()));
					continue;
				}
				
				
				// OBJFILENAME
				if (str.find("ObjFilename") != -1) {
					int begin_index = str.find("ObjFilename")+strlen("ObjFilename")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					item->setObjFilename(str);
					continue;
				}
				
				// WMTFFILENAME
				if (str.find("WmtfFilename") != -1) {
					int begin_index = str.find("WmtfFilename")+strlen("WmtfFilename")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					item->setWmtfFilename(str);
					continue;
				}
				
				// TRANSLATION
				if (str.find("Translate") != -1) {
					int begin_index = str.find("Translate")+strlen("Translate")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					
					// get translation value
					int i = 0;
					float t[3];
					token = strtok((char*)str.c_str(), " ");
					while (token != NULL) {
						t[i] = atof(token);
						token = strtok(NULL, " ");
						i++;
					}
					item->setTranslate(t[0], t[1], t[2]);
					continue;
				}
				
				// END
				if (str.find("end") != -1) {
					
				//	item->printData();
					
					itemList.push_back(*item);
					continue;
				}
			};
			
		}
	}
	i_file.close();
}

vector<Items> ItemList::getItemList() {
	return itemList;
}

Items * ItemList::getItemByID(int ID) {
	int i;
	for (i = 0; i < itemList.size(); i++) {
		if (itemList[i].getItemID() == ID) {
			return &itemList[i];
		}
	}
	return NULL;
}

Items * ItemList::getItemByTitle(string title) {
	int i;
	for (i = 0; i < itemList.size(); i++) {
		if (itemList[i].getItemTitle() == title) {
			return &itemList[i];
		}
	}
	return NULL;
}

Items * ItemList::getItemByType(int type) {
	int i;
	for (i = 0; i < itemList.size(); i++) {
		if (itemList[i].getItemType() == type) {
			return &itemList[i];
		}
	}
	return NULL;
}

int ItemList::getItemCount() {
	return itemList.size();
}